package cs5226.actions;

import java.awt.Color;
import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;

import com.opensymphony.xwork2.ActionSupport;

import cs5226.dao.*;
import cs5226.model.*;
import cs5226.tags.ThresholdUtil;

public class RedoLogAction extends ActionSupport implements
		ServletRequestAware {
	private HttpServletRequest request = null;

	
	public String redoLogEntry() throws Exception {
		try {
			List<PercentageSingleValueStatistics> ahList = RedoLogDAO.getRedoAllocationHit();			
			List<SingleValueStatistics> rlsiwList = RedoLogDAO.getRedoLogSwitchIncompleteWait();
			
			request.setAttribute(ThresholdUtil.REDO_BUFFER_ALLOCATION_HIT, ahList);	
			prepareChart("RbahGraph", ThresholdUtil.REDO_BUFFER_ALLOCATION_HIT, ahList);
			
			request.setAttribute(ThresholdUtil.REDO_LOG_SWITCH_INCOMPLETE_CHECKPOINT_WAIT_TIME, rlsiwList);	
			prepareChart("RlsicwtGraph", ThresholdUtil.REDO_LOG_SWITCH_INCOMPLETE_CHECKPOINT_WAIT_TIME, rlsiwList);
			
			if (request.getSession().getAttribute("awrsetting") == null) {
				AwrSetting awrsetting = ConfigurationDAO.getAwrSetting();				
				request.getSession().setAttribute("awrsetting", awrsetting);
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("error", e.getMessage());
		}

		return super.SUCCESS;
	}
	

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}

	public void prepareChart(String name, String statistics, List<? extends SingleValueStatistics> statList) throws Exception {
		ThresholdBean threshold = ThresholdUtil.getThresholdValues(statistics, request);
		
		
		DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
		// line name
		String unhealthy = "Warning";
		String critical = "Critical";
		String value = statistics;

		for (int i=statList.size() -1; i>=0; i--) {
			SingleValueStatistics stat = statList.get(i);
			linedataset.addValue(threshold.getUnhealthy(), unhealthy, "" + stat.getTs().getTime());
			linedataset.addValue(threshold.getCritical(), critical, "" + stat.getTs().getTime());
			linedataset.addValue(stat.getValue(), value, "" + stat.getTs().getTime());
		}

		
		
		// create the chart
		JFreeChart chart = ChartFactory.createLineChart(statistics,//title
				"time", // x label name
				"value", // y label name
				linedataset, // data
				PlotOrientation.VERTICAL, // orientation
				true, // include legend
				true, // tooltips
				false // urls
				);
		CategoryPlot plot = chart.getCategoryPlot();
		
		CategoryItemRenderer renderer = plot.getRenderer();
		renderer.setSeriesPaint(0, Color.yellow);
		renderer.setSeriesPaint(1, Color.red);
		renderer.setSeriesPaint(2, Color.black);
		
		// customise the range axis...
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		rangeAxis.setAutoRangeIncludesZero(true);
		rangeAxis.setUpperMargin(0.20);
		rangeAxis.setLabelAngle(Math.PI / 2.0);

		StandardEntityCollection sec = new StandardEntityCollection();
		ChartRenderingInfo info = new ChartRenderingInfo(sec);
		String filename = ServletUtilities.saveChartAsJPEG(chart, 450, 250,
				info, request.getSession());

		request.setAttribute(name, filename);

	}
}
